package com.zhiqianbao.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.UserJob;
import com.zhiqianbao.shared.SharedPreferanceUtils;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.PermissionDialog;
import com.zhiqianbao.utils.PermissionsManager;
import com.zhiqianbao.utils.T;
import com.zhiqianbao.utils.WorkIndustryDataUtils;
import java.util.List;

/**
 * Created by liang on 2015/12/29.
 */
public class JobInfoActivity extends BaseActivity {

    /**
     * 职位
     */
    TextView textviewDutyContent;
    /**
     * 单位名称
     */
    TextView textviewUnitContent;
    /**
     * 所在城市
     */
    TextView textviewThecityContent;
    /**
     * 单位地址
     */
    TextView textviewUnitAddressContent;
    /**
     * 单位电话
     */
    TextView textviewUnitTelContent;
    /**
     * 更改工作信息按钮
     */
    Button buttonChangeJobInfo;
    SharedPreferanceUtils sharedPreferanceUtils;
    /**
     * 名称
     */
    TextView textviewUnitTitle;
    /**
     * 地址
     */
    TextView textviewUnitAddressTitle;
    /**
     * 电话
     */
    TextView textviewUnitTelTitle;
    private TextView textview_msg;

    UserJob userJob;
    String profession="";
    WorkIndustryDataUtils dataUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_job);
        sharedPreferanceUtils = new SharedPreferanceUtils(this);
        dataUtils=new WorkIndustryDataUtils(getResources());
        init();
        initView();
        checkPermission_v4(Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,Manifest.permission.WRITE_SETTINGS);
    }

    private PermissionDialog permissionDialog;
    private PermissionsManager perManager;
    private  boolean checkPermission_v4(final String... permission) {
        if (perManager == null)
            perManager = new PermissionsManager(this, new PermissionsManager.PermissionListener() {
                // 添加权限请求返回监听器并内置了一个二次提示框
                @Override
                public void getRequestPermissionCallBack(String[] permissions, boolean isAgree) {
                    // 权限没有全部获取
                    if (!isAgree) {
                        final String[] p = permissions;
                        // 向用户弹出一个提示框,包含确定和取消两个按钮
                        if (permissionDialog == null)
                            permissionDialog = new PermissionDialog(getApplicationContext(), new PermissionDialog.PermissionCheckListener() {

                                @Override
                                public void userChoosed(boolean isChooseOk) {
                                    if (isChooseOk) {// 点击确定
                                        // 先检查一下有没有完全禁止，有的话那就拜拜了您那~~ 让系统安全应用去处理吧
                                        checkShouldShowPermission(p);
                                        // 没有就再次检查指导结束
                                        checkPermission_v4(permission);
                                    } else {// 点击取消
                                        // 关闭页面
                                    }
                                }
                            });
                        permissionDialog.show();
                    }
                }
            });
        // 检查是否已有权限
        return perManager.checkPermission(permission);
    }

    /**
     * 检查是否可以显示权限授予对话框
     *
     * @param permissions
     * @author Wall
     */
    public void checkShouldShowPermission(String[] permissions) {
        if (perManager != null) {
            // 检查是否已被禁止
            boolean isNeedShow = perManager.checkShouldShowPermission(permissions);
            if (isNeedShow) {
                T.getInstanse(this).showLong(R.string.exception_nopermission);
            }
        }
    }

    private void initView() {
         textviewDutyContent=(TextView)findViewById(R.id.textview_duty_content);
         textviewUnitContent=(TextView)findViewById(R.id.textview_unit_content);
         textviewThecityContent=(TextView)findViewById(R.id.textview_thecity_content);
         textviewUnitAddressContent=(TextView)findViewById(R.id.textview_unit_address_content);
         textviewUnitTelContent=(TextView)findViewById(R.id.textview_unit_tel_content);
         buttonChangeJobInfo=(Button)findViewById(R.id.button_change_job_info);
         textviewUnitTitle=(TextView)findViewById(R.id.textview_unit_title);
         textviewUnitAddressTitle=(TextView)findViewById(R.id.textview_unit_address_title);
         textviewUnitTelTitle=(TextView)findViewById(R.id.textview_unit_tel_title);
        textview_msg = (TextView) findViewById(R.id.textview_msg);
        //电话号码
        try{
            String userId = Constants.LOGIN_USER.getId().toString();
            final List<UserJob> userJobs = UserJob.findWithQuery(UserJob.class, "select * from user_job where user_id = ?", userId);
            if (userJobs!=null && userJobs.size()>0){
                userJob = userJobs.get(0);
                Log.i("我现在是",userJob.toString());
                textviewDutyContent.setText(userJob.getJobName());
                textviewUnitAddressContent.setText(userJob.getAddress());
//                textviewDutyContent.setText(userJob.getJob());
                textviewUnitTelContent.setText(userJob.getPhone());
                textviewThecityContent.setText(userJob.getProvince()+userJob.getCity()+userJob.getArea());
                textviewUnitContent.setText(userJob.getCompany());
                String[] array_work_msg = getResources().getStringArray(R.array.array_work_msg);
                profession=sharedPreferanceUtils.getUserJobProfession();
                if(!TextUtils.isEmpty(profession)) {
                    textview_msg.setText(dataUtils.getWorkMsg(profession));
                }
            }
//            }
//            sharedPreferanceUtils.getAdress();
//            String companey = sharedPreferanceUtils.getCompaney();
//            String jobNumber = sharedPreferanceUtils.getJobNumber();
//            String job = sharedPreferanceUtils.getJob();
//            sharedPreferanceUtils.getProvince();
//            String area1 = sharedPreferanceUtils.getArea1();
//            sharedPreferanceUtils.setCity12(city);
//            String city1 = sharedPreferanceUtils.getCity();
//            sharedPreferanceUtils.setPhone(phone);
//            if (userJob==null){
//                textviewUnitTitle.setText(companey);
//                textviewUnitAddressTitle.setText(area1);
//                textviewDutyContent.setText(job);
//                textviewUnitTelContent.setText(jobNumber);
//                textviewThecityContent.setText(city1);
//            }

            if (!TextUtils.isEmpty(userJob.toString())){
                if ("学生".equals(userJob.getJobName())) {
                    textviewUnitTitle.setText("学校名称");
                    textviewUnitAddressTitle.setText("学校地址");
                    textviewUnitTelTitle.setText("学校电话");
                } else {
                    textviewUnitTitle.setText("单位名称");
                    textviewUnitAddressTitle.setText("单位地址");
                    textviewUnitTelTitle.setText("单位电话");
                    textviewDutyContent.setText(userJob.getJobName());
                    textviewUnitContent.setText(userJob.getCompany());
                    String city = "";
                    if (userJob.getProvince().equals(userJob.getCity())) {
                        city = userJob.getProvince() + userJob.getArea();
                    } else {
                        city = userJob.getProvince() + userJob.getCity() + userJob.getArea();
                    }
                    textviewThecityContent.setText(city);
                    textviewUnitAddressContent.setText(userJob.getAddress());
                    textviewUnitTelContent.setText(userJob.getPhone());
                }
            }
        }catch (Exception e){

        }

//        String job = SharedPreferanceUtils.getJob();
//        Log.i("这个人的job",job.toString());


        buttonChangeJobInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(JobInfoActivity.this, UpdateJobActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("userJob", userJob);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 1 : {
                UserJob newUserJob = (UserJob) data.getSerializableExtra("userJob");
                userJob = newUserJob;
                if ("学生".equals(newUserJob.getJobName())) {
                    textviewUnitTitle.setText("学校名称");
                    textviewUnitAddressTitle.setText("学校地址");
                    textviewUnitTelTitle.setText("学校电话");
                } else {
                    textviewUnitTitle.setText("单位名称");
                    textviewUnitAddressTitle.setText("单位地址");
                    textviewUnitTelTitle.setText("单位电话");
                }

                textviewDutyContent.setText(newUserJob.getJobName());
                textviewUnitContent.setText(newUserJob.getCompany());
                String city = "";
                if (newUserJob.getProvince().equals(newUserJob.getCity())) {
                    city = newUserJob.getProvince() + newUserJob.getArea();
                } else {
                    city = newUserJob.getProvince() + newUserJob.getCity() + newUserJob.getArea();
                }
                textviewThecityContent.setText(city);
                textviewUnitAddressContent.setText(newUserJob.getAddress());
                textviewUnitTelContent.setText(newUserJob.getPhone());
                String[] array_work_msg = getResources().getStringArray(R.array.array_work_msg);
                profession=sharedPreferanceUtils.getUserJobProfession();
                if(!TextUtils.isEmpty(profession)) {
                    textview_msg.setText(dataUtils.getWorkMsg(profession));
                }
            }
        }
    }
}
